package ma.ensao.evalmetrics.controller;


import java.util.List;
import java.util.Map;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import ma.ensao.evalmetrics.model.Admin;
import ma.ensao.evalmetrics.model.User;
import ma.ensao.evalmetrics.util.HibernateUtil;
/*
* Gestion des Utilisateurs et Administrateurs
*/
 
public class UserManager extends HibernateUtil {
	private Map<String, Object> connexion;
	private Long Id;
	// Ajouter un user � la base de donn�es
	public User add(User user) {
		System.out.println("UserManager add"+user.getNom()+"\n");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		return user;
	}
	// Modifier un user
	public User modifier(User user) {
		//Long Id= (long) 20;
		System.out.println("UserManager modifier");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User userUpdate =(User) session.load(User.class, user.getId());
		userUpdate.setNom(user.getNom());
		userUpdate.setPrenom(user.getPrenom());
		userUpdate.setEmailId(user.getEmailId());
		userUpdate.setStatut(user.getStatut());
		userUpdate.setTelephone(user.getTelephone());
		userUpdate.setUserLogin(user.getUserLogin());
		userUpdate.setUserPassword(user.getUserPassword());
		try {
			/*users = (User)session.createQuery("update User set nom = :"+user.getNom()+
					"prenom = :" +user.getPrenom()+
					"emailId = :" +user.getEmailId()+
					"telephone = :" +user.getTelephone()+
					"statut = :" +user.getStatut()+
					"userLogin = :" +user.getUserLogin()+
					"userPassword = :" +user.getUserPassword()+
					"where id = :"+user.getId()).list();*/
			session.update(userUpdate);
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return userUpdate;
		
	}
	// R�cuperation des user existants dans la base de donn�es
	public List<User> list() {
		System.out.println("UserManager liste");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>)session.createQuery("from User order by id desc").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return users;
	}
	// R�cuperation d'un user avec son ID
	public List<User> getSpecificUser(Long Id) {
		System.out.println("UserManager modif");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("from User where id="+Id+"").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return  users;
	}
	// Suppression d'un User
	public User delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		User user = (User) session.load(User.class, id);
		if(null != user) {
			session.delete(user);
		}
		session.getTransaction().commit();
		return user;
	}
	// Chercher l'existance d'un Admin dans la base des donn�es
	public boolean exists(String login) {
		boolean test;
		System.out.println("AdminManager exists "+login+"\n");
		List results= null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		StringBuffer queryString = new StringBuffer();
		queryString.append("Select id from Admin admin ")
		  .append("where admin.login= :logi");
		try {
			Query query = session.createQuery(queryString.toString());
			query.setString("logi",login);
			results =query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		 if(results.size() != 0) {
			 long id=(Long) results.get(0);
			 Admin admin = (Admin) session.get(Admin.class, id);
			 session.getTransaction().commit();
			if (admin != null){
				 test= true;
			 }else{
				 test= false;
			 }
		 }else{
			 test= userExists(login);
		 }
		return test;
	}
	// Chercher l'existance d'un user dans la base des donn�es
	public boolean userExists(String login) {
		System.out.println("AdminManager Connect User "+login+"\n");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		StringBuffer queryString = new StringBuffer();
		queryString.append("Select id from User user ")
		  .append("where user.userLogin= :logi");
		
		Query query = session.createQuery(queryString.toString());
		query.setString("logi",login);
		List results =query.list();
		
		 boolean test;
		if(results.size() != 0) {
			System.out.println("inside+"+results.size());
			 long id=(Long) results.get(0);
			 User user = (User) session.get(User.class, id);
			 session.getTransaction().commit();
			if (user != null){
				System.out.println("inside+ !null");
				test= true;
			 }else{
				 System.out.println("inside null");
				 test=false;
			 }
		 }else{
			System.out.println("inside+ nullll"+results.size());
			 test=false;
		 }
		return test;
	}
}
